package com.sinvo.oa.controller;

import com.sinvo.oa.result.R;
import com.wf.captcha.SpecCaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * @Description 验证码控制器
 */
@Api(tags = "验证码生成接口")
@RestController
@RequestMapping
public class CaptchaController {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 生成验证码
     * @return img code
     */
    @ApiOperation(value = "生成验证码")
    @GetMapping("/captcha")
    public R captcha() {
        //大小
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
        //验证码小写
        String verCode = specCaptcha.text().toLowerCase();
        // 存入redis并设置过期时间为5分钟
        redisTemplate.opsForValue().set("code",verCode,5, TimeUnit.MINUTES);
        // 将key和base64返回给前端
        HashMap<String, String> map = new HashMap<>();
        //图片
        map.put("image", specCaptcha.toBase64());
        //验证码value
        map.put("verCode",verCode);
        return R.success(map);
    }
}
